﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp.View.LineManage
{
    /// <summary>
    /// UC_CreateLines.xaml 的交互逻辑
    /// </summary>
    public partial class UC_CreateLines : UserControl
    {
        BLL.UCStationManage.UC_StationManageClient myStationManage = new BLL.UCStationManage.UC_StationManageClient();
        public int intLineID = 0;//公共线路ID
        public string strType;//线路状态（停用，为停用）
        public UC_CreateLines()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 页面加载时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            SelectLine();
        }
        #region 自定义生成控件
        /// <summary>
        /// 生成线路
        /// </summary>
        public void SelectLine()
        {
            try
            {
                //1、移除控件子元素
                if (WPLines != null)
                {
                    WPLines.Children.Clear();
                }
                //2、获取数据
                DataTable dtLine = myStationManage.SelectLine().Tables[0];
                if (dtLine.Rows.Count > 0)
                {
                    //3、循环自定义控件布局
                    for (int i = 0; i < dtLine.Rows.Count; i++)
                    {
                        //(主键ID、线路名称、总站数、停用否)
                        int intID = Convert.ToInt32(dtLine.Rows[i]["lineID"]);
                        string strLineName = dtLine.Rows[i]["lineName"].ToString().Trim();
                        string strStopNo = dtLine.Rows[i]["stopNo"].ToString().Trim();
                        //总站数
                        DataTable dtDetailLine = myStationManage.SelectDetailLine(intID).Tables[0];
                        string strCount = "总站数:" + (dtDetailLine.Rows.Count + 1);
                        #region 自定义布局
                        //(1)边框
                        //Width="125" Height="105" Background="White" BorderBrush="SkyBlue" BorderThickness="1,1,1,1" CornerRadius="5" Margin="5"
                        Border border = new Border();
                        border.Width = 125;
                        border.Height = 105;
                        border.Background = Brushes.White;//背景色
                        border.BorderBrush = Brushes.SkyBlue;//边框颜色
                        border.BorderThickness = new Thickness(1);//边框粗度
                        border.CornerRadius = new CornerRadius(5);//圆角
                        border.Margin = new Thickness(5);

                        //(2)网格布局
                        //Width="120" Height="100" MouseRightButtonDown="grd_MouseRightButtonDown" MouseLeftButtonDown="grd_MouseLeftButtonDown" MouseEnter="grd_MouseEnter" MouseLeave="grd_MouseLeave"
                        //ToolTip="" Tag=""
                        Grid grid = new Grid();
                        grid.Width = 120;
                        grid.Height = 100;
                        //事件：按下鼠标右键
                        grid.MouseRightButtonDown += new MouseButtonEventHandler(grd_MouseRightButtonDown);
                        //事件：按下鼠标左键
                        grid.MouseLeftButtonDown += new MouseButtonEventHandler(grd_MouseLeftButtonDown);
                        //事件：鼠标移入
                        grid.MouseEnter += new MouseEventHandler(grd_MouseEnter);
                        //事件：鼠标移开
                        grid.MouseLeave += new MouseEventHandler(grd_MouseLeave);
                        grid.ToolTip = strStopNo;//停用否
                        grid.Tag = intID;//主键
                        /*
                         * <Grid.RowDefinitions>
                            <RowDefinition Height="3*"/>
                            <RowDefinition Height="2*"/> 
                        </Grid.RowDefinitions>
                         */
                        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(3, GridUnitType.Star) });
                        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(2, GridUnitType.Star) });
                        //（2）文本-线路名
                        //Grid.Row="0" Name="txt_LineName" Text="广州-南沙" Foreground="SkyBlue" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center"
                        TextBlock txt_LineName = new TextBlock();
                        //单元格（第一行，第一列）线路  
                        txt_LineName.SetValue(Grid.RowProperty, 0);//附加属性Grid.Row： 
                        txt_LineName.SetValue(Grid.ColumnProperty, 0);//附加属性Grid.Column：
                        txt_LineName.Text = strCount;
                        txt_LineName.Foreground = strStopNo == "停用" ? Brushes.Red : Brushes.SkyBlue;//停用（红色），未停用
                        txt_LineName.FontSize = 18;
                        txt_LineName.HorizontalAlignment = HorizontalAlignment.Center;//水平居中
                        txt_LineName.VerticalAlignment = VerticalAlignment.Center;//垂直居中

                        //（4）站点数
                        //Grid.Row="1" Name="txt_DetailCount" Text="总站数:10" Foreground="SkyBlue" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"
                        TextBlock txt_DetailCount = new TextBlock();
                        txt_DetailCount.SetValue(Grid.RowProperty, 1);//附加属性Grid.Row：       
                        txt_DetailCount.SetValue(Grid.ColumnProperty, 0);//附加属性Grid.Column：
                        txt_DetailCount.Text = strLineName;
                        txt_DetailCount.Foreground = strStopNo == "停用" ? Brushes.Red : Brushes.SkyBlue;
                        txt_DetailCount.FontSize = 16;
                        txt_DetailCount.HorizontalAlignment = HorizontalAlignment.Center;
                        txt_DetailCount.VerticalAlignment = VerticalAlignment.Center;

                        grid.Children.Add(txt_LineName);
                        grid.Children.Add(txt_DetailCount);
                        border.Child = grid;
                        WPLines.Children.Add(border);
                        #endregion

                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// Grid 鼠标右键点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void grd_MouseRightButtonDown(object sender,MouseButtonEventArgs e)
        {
            ContextMenu cm = this.FindResource("cmButton") as ContextMenu;
            //目标
            cm.PlacementTarget = sender as Button;
            //位置
            cm.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;
            //显示菜单
            cm.IsOpen = true;
            //获取网格信息
            Grid dg = sender as Grid;
            //记录点击的线路ID（修改，删除使用到主键ID）
            intLineID = Convert.ToInt32(dg.Tag);
            strType = dg.ToolTip.ToString().Trim();//线路状态

        }
        /// <summary>
        /// 2.2 Grid 鼠标左键(绑定右边表格：线路明细)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void grd_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
        {
            Grid grid = sender as Grid;
            //获取线路ID
            int intThisID = Convert.ToInt32(grid.Tag);
            //查询线路明细
            DataTable dtDetailLine = myStationManage.SelectDetailLine(intThisID).Tables[0];
            //绑定数据
            dgStation.ItemsSource = dtDetailLine.DefaultView;
        }
        /// <summary>
        /// 鼠标移入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void grd_MouseEnter(object sender,MouseEventArgs e)
        {
            Grid grid = sender as Grid;
            grid.Background = Brushes.SkyBlue;
            //批量获取子元素
            List<TextBlock> list = Tools.Utils.GetChildObjects.GetChildObject<TextBlock>(grid);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Foreground = Brushes.White;//字体颜色白色
            }
        }
        /// <summary>
        /// 鼠标移出事件 Grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void grd_MouseLeave(object sender,MouseEventArgs e)
        {
            Grid grid = sender as Grid;
            strType = grid.ToolTip.ToString().Trim();//状态
            grid.Background = Brushes.White;
            //批量获取子元素
            List<TextBlock> list = Tools.Utils.GetChildObjects.GetChildObject<TextBlock>(grid);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Foreground = strType == "停用" ? Brushes.Red : Brushes.SkyBlue;
            }
        }

        #endregion
        /// <summary>
        /// 新增生成线路(鼠标右键菜单)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mi_Insert_Click(object sender, RoutedEventArgs e)
        {
            WD_InsertCreateLine myCreateLine = new WD_InsertCreateLine();
            myCreateLine.ShowDialog();
            //2、刷新线路数据
            SelectLine();
        }

        private void mi_Stop_Click(object sender, RoutedEventArgs e)
        {

        }

        private void mi_Delete_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dgStation_LoadingRow(object sender, DataGridRowEventArgs e)
        {

        }
    }
}
